package com.tg.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.joooonho.SelectableRoundedImageView;
import com.tange.base.toolkit.DimenUtil;
import com.tange.base.toolkit.TGThreadPool;
import com.tg.app.R;
import com.tg.app.activity.CloudServiceActivity;
import com.tg.app.activity.base.DeviceListBaseActivity;
import com.tg.app.activity.device.DeviceListActivity;
import com.tg.app.bean.LocalThumbnail;
import com.tg.app.helper.ActivityHelper;
import com.tg.app.util.LocalThumbnailUtils;
import com.tg.app.util.LogUtils;
import com.tg.appcommon.android.ImageUtils;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceItem;

import java.util.List;


public class DeviceListAdapter extends DeviceAdapter {
    private static final String TAG = "DeviceListAdapter";
    public Drawable drawableOnline;
    public Drawable drawableOffline;


    public DeviceListAdapter(Context context, List<DeviceItem> list) {
        super(context, list);
        drawableOnline = ContextCompat.getDrawable(mContext, R.drawable.ic_tange_online);
        drawableOffline = ContextCompat.getDrawable(mContext, R.drawable.ic_tange_offline);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == DEVICE_ITEM_TYPE) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_device_item, null);
            return new DeviceViewHolder(view);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_addmore_device, null);
            return new AddViewHolder(view);
        }
    }

    @Override
    protected int roundRadiusResId() {
        return R.dimen.global_customize_fixed_global_rounded_radius;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()){
            onBindViewHolder(holder,position);
        }else {
            onViewHolderBind(holder,position);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position){
        onViewHolderBind(holder,position);
    }
    public void onViewHolderBind(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position){
        if (holder instanceof DeviceViewHolder) {
            final DeviceViewHolder deviceViewHolder = (DeviceViewHolder) holder;
            final DeviceItem item = mList.get(position);
            deviceViewHolder.previewlayout.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position, DeviceListActivity.PUSH_TARGET_LIVE);
                }
            });
            deviceViewHolder.previewiv.setOnClickListener(view -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position, DeviceListActivity.PUSH_TARGET_LIVE);
                }

            });

            if (deviceViewHolder.settingIv != null) {
                deviceViewHolder.settingIv.setOnClickListener(view -> {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position, DeviceListBaseActivity.OPEN_SETTING);
                    }
                });
            }

            LocalThumbnail localThumbnail = LocalThumbnailUtils.getInstance().getLocalThumbnail(item.uuid);

            if (localThumbnail != null && localThumbnail.bitmap != null) {
                ImageUtils.loadImage(mContext, deviceViewHolder.previewiv, localThumbnail.bitmap, false, false);
            }

            ImageUtils.loadImage(mContext, deviceViewHolder.previewiv, item.image_path, false, false, bitmap -> TGThreadPool.execute(() -> LocalThumbnailUtils.getInstance().saveLocalThumbnailWithCopy(bitmap, item.uuid)));

            TGLog.d(TAG, "localThumbnail = " + localThumbnail + ", uuid = " + item.uuid + ", name = " + item.name);
            deviceViewHolder.ivMore.setOnClickListener(view -> {
                LogUtils.onEventClickByName("device_list", "list_more");
                if (onItemClickListener != null) {
                    openMore(item);
                }
            });

            deviceViewHolder.tvDeviceItem.setOnClickListener(v -> ActivityHelper.openWebService(mContext, item, CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE_SIM));

            deviceViewHolder.tvDeviceName.setText(item.name);
            deviceViewHolder.tvStatus.setText(item.isOnline() ? R.string.online_again : R.string.offline);
            Drawable drawable = item.isOnline() ? drawableOnline : drawableOffline;
            deviceViewHolder.tvStatus.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
            deviceViewHolder.tvStatus.setCompoundDrawablePadding(5);

            deviceViewHolder.previewiv.setVisibility(View.VISIBLE);
        } else {
            AddViewHolder addDeviceViewHolder = (AddViewHolder) holder;
            addDeviceViewHolder.addBtn.setOnClickListener(view -> {
                LogUtils.onEventClickByName("device_list", "list_add");
                if (onNoItemClickListener != null) {
                    onNoItemClickListener.onViewClick();
                }
            });
        }
    }


    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
        if (holder instanceof DeviceViewHolder) {
            DeviceViewHolder h = (DeviceViewHolder) holder;
            if (h.previewiv != null) {
                h.previewiv.setTag(R.id.image_load_tag, "");
            }
        }
    }

    private class DeviceViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout previewlayout;

        SelectableRoundedImageView previewiv;

        TextView tvDeviceName;
        TextView tvStatus;
        RelativeLayout relCarFunction;
        ImageView ivMore;

        ImageView settingIv;

        TextView tvDeviceItem;
        DeviceViewHolder(View itemView) {
            super(itemView);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.leftMargin = DimenUtil.dp2px(mContext, 20);
            layoutParams.rightMargin = DimenUtil.dp2px(mContext, 20);
            itemView.setLayoutParams(layoutParams);

            tvDeviceItem = itemView.findViewById(R.id.tv_device_list_item);
            previewlayout = itemView.findViewById(R.id.previewlayout);
            previewiv = itemView.findViewById(R.id.previewiv);
            tvDeviceName = itemView.findViewById(R.id.tv_device_item_name);
            tvStatus = itemView.findViewById(R.id.tv_device_item_status);
            relCarFunction = itemView.findViewById(R.id.rel_device_car_function);
            ivMore = itemView.findViewById(R.id.btn_device_list_item_more);
            settingIv = itemView.findViewById(R.id.btn_device_list_item_setting);
        }
    }



    private class AddViewHolder extends RecyclerView.ViewHolder {

        ImageButton addBtn;

        public AddViewHolder(@NonNull View itemView) {
            super(itemView);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.leftMargin = DimenUtil.dp2px(mContext, 20);
            layoutParams.rightMargin = DimenUtil.dp2px(mContext, 20);
            itemView.setLayoutParams(layoutParams);
            addBtn = itemView.findViewById(R.id.btn_device_list_item_add);
        }
    }


}

